package org.bbs.demo.topic.api;

import org.bbs.demo.common.BBSConstants;
import org.bbs.demo.common.Result;
import org.bbs.demo.topic.dao.ShowByTime;
import org.bbs.demo.topic.dao.ShowTopSexy;
import org.bbs.demo.topic.dao.ShowTopicListDao;
import org.bbs.demo.topic.dao.TopicOFuser;
import org.bbs.demo.topic.dto.ShowTopicDto;
import org.bbs.demo.topic.dto.TopSexyDto;
import org.bbs.demo.topic.dto.UserTopic;
import org.bbs.demo.topic.model.Topic;
import org.bbs.demo.topic.model.TopicList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@RequestMapping("/show")
@Transactional
public class ShowTopicApi {

    @Autowired
    private ShowTopicListDao showTopicListDao;

    @PostMapping("/getTopic")
    public Result showTopic(@RequestBody ShowTopicDto showTopicDto){

        try {
            //ArrayList<ArrayList<Topic>> topicContentList = new ArrayList<>();
            ArrayList<Topic> topicContentList = new ArrayList<>();
            for (int i = 0; i <= showTopicDto.getTopicList().size() - 1 ; i++ ){
                /**ArrayList<Topic> topiclist = showTopicListDao.findTopic(showTopicDto.getTopicList().get(i));
                topicContentList.add(topiclist);**/
                String topicid = showTopicDto.getTopicList().get(i);
                Topic topic = showTopicListDao.findTopic(topicid);
                topic.setPicture_addr(showTopicListDao.findPicture(topicid));
                topicContentList.add(topic);
                showTopicListDao.cliksSexy(topicid);
                showTopicListDao.addClik(topicid);
            }
            return Result.OK("获取成功",topicContentList);
        }catch (DuplicateKeyException e){
            return Result.err(BBSConstants.CODE_ERR_BUSINESS,"ERROR");
        }
    }

    @Autowired
    private ShowTopSexy showTopSexy;

    @PostMapping("/getTopsexy")
    public Result getTop(@RequestBody TopSexyDto topSexyDto){

        try {
            return Result.OK("查询成功",showTopSexy.showTopSexy(topSexyDto.getNum()));
        }catch (DuplicateKeyException e){
            return Result.err(BBSConstants.CODE_ERR_BUSINESS,"ERROR");
        }
    }

    @Autowired
    private TopicOFuser topicOFuser;

    @PostMapping("/getUserTopic")
    public Result getUserTopic(@RequestBody UserTopic userTopic){
        try {
            return Result.OK("查询成功",topicOFuser.showTopicOFuser(userTopic.getUser_id()));
        }catch (DuplicateKeyException e){
            return Result.err(BBSConstants.CODE_ERR_BUSINESS,"ERROR");
        }
    }

    @Autowired
    private ShowByTime showByTime;

    @GetMapping("/getTopicByTime")
    public Result getTopicByTime(){
        try {
            return Result.OK("查询成功",showByTime.showTopicByTime());
        }catch (DuplicateKeyException e){
            return Result.err(BBSConstants.CODE_ERR_BUSINESS,"ERROR");
        }
    }
}
